// 多语言支持 - 翻译数据
const translations = {
    "zh-CN": {
        // 页面标题
        "title": "个人作品集",
        
        // 导航
        "nav-home": "首页",
        "nav-about": "关于我",
        "nav-projects": "项目",
        "nav-contact": "联系方式",
        
        // 首页
        "hero-title": "欢迎来到我的作品集",
        "hero-subtitle": "展示我的创意项目和技术技能",
        "hero-button": "查看项目",
        
        // 关于我
        "about-title": "关于我",
        "about-role": "Python Developer",
        "about-description": "擅长消息中间件、实时数据处理、高可用架构设计。喜欢Rust、幸运星、孤独摇滚、摇曳露营。",
        "experience-title": "专业经历",
        "achievements-title": "个人成就",
        
        // 技能标签
        "skill-python": "Python",
        "skill-mongodb": "MongoDB",
        "skill-nextjs": "Next.js",
        "skill-elasticsearch": "ElasticSearch",
        "skill-rust": "Rust",
        
        // 教育经历
        "education-tsinghua": "清华大学",
        "education-bachelor": "本科",
        "education-stanford": "斯坦福大学",
        "education-master": "硕士",
        
        // 工作经历
        "work-google": "Google",
        "work-openai": "OpenAI",
        "work-position": "软件工程师",
        
        // 成就
        "achievement-1": "清华大学优秀毕业生",
        "achievement-2": "微软MVP",
        "achievement-3": "华为开发者认证",
        
        // 项目
        "projects-title": "我的项目",
        "project-name": "项目名称",
        "project-description": "项目描述和主要功能特点。",
        "project-link": "查看详情",
        
        // 技能
        "skills-title": "技能专长",
        "skill-frontend": "前端开发",
        "skill-backend": "后端开发",
        "skill-database": "数据库设计",
        "skill-uiux": "UI/UX 设计",
        
        // 联系表单
        "contact-title": "联系我",
        "contact-info-title": "联系方式",
        "contact-name": "姓名",
        "contact-email": "邮箱",
        "contact-message": "留言",
        "contact-send": "发送消息",
        "contact-personal-website": "个人网页",
        "contact-email-title": "邮箱",
        
        // 表单消息
        "form-success": "留言发送成功！我会尽快回复您。",
        "form-error": "请填写所有必填字段。",
        
        // 页脚
        "footer-copyright": "个人作品集. 保留所有权利.",
        
        // 社交媒体
        "social-github": "GitHub",
        "social-linkedin": "LinkedIn",
        "social-twitter": "Twitter"
    },
    "en": {
        // 页面标题
        "title": "Personal Portfolio",
        
        // 导航
        "nav-home": "Home",
        "nav-about": "About Me",
        "nav-projects": "Projects",
        "nav-contact": "Contact",
        
        // 首页
        "hero-title": "Welcome to My Portfolio",
        "hero-subtitle": "Showcasing my creative projects and technical skills",
        "hero-button": "View Projects",
        
        // 关于我
        "about-title": "About Me",
        "about-role": "Python Developer",
        "about-description": "Specializing in message middleware, real-time data processing, and high-availability architecture design. Love Rust, Lucky Star, Bocchi the Rock, and Yuru Camp.",
        "experience-title": "Professional Experience",
        "achievements-title": "Achievements",
        
        // 技能标签
        "skill-python": "Python",
        "skill-mongodb": "MongoDB",
        "skill-nextjs": "Next.js",
        "skill-elasticsearch": "ElasticSearch",
        "skill-rust": "Rust",
        
        // 教育经历
        "education-tsinghua": "Tsinghua University",
        "education-bachelor": "Bachelor's Degree",
        "education-stanford": "Stanford University",
        "education-master": "Master's Degree",
        
        // 工作经历
        "work-google": "Google",
        "work-openai": "OpenAI",
        "work-position": "Software Engineer",
        
        // 成就
        "achievement-1": "Tsinghua University Outstanding Graduate",
        "achievement-2": "Microsoft MVP",
        "achievement-3": "Huawei Developer Certification",
        
        // 项目
        "projects-title": "My Projects",
        "project-name": "Project Name",
        "project-description": "Project description and main features.",
        "project-link": "View Details",
        
        // 技能
        "skills-title": "Skills",
        "skill-frontend": "Frontend Development",
        "skill-backend": "Backend Development",
        "skill-database": "Database Design",
        "skill-uiux": "UI/UX Design",
        
        // 联系表单
        "contact-title": "Contact Me",
        "contact-info-title": "Contact Information",
        "contact-name": "Name",
        "contact-email": "Email",
        "contact-message": "Message",
        "contact-send": "Send Message",
        "contact-personal-website": "Personal Website",
        "contact-email-title": "Email",
        
        // 表单消息
        "form-success": "Message sent successfully! I will reply to you as soon as possible.",
        "form-error": "Please fill in all required fields.",
        
        // 页脚
        "footer-copyright": "Personal Portfolio. All rights reserved.",
        
        // 社交媒体
        "social-github": "GitHub",
        "social-linkedin": "LinkedIn",
        "social-twitter": "Twitter"
    }
};

// 当前语言
let currentLanguage = localStorage.getItem('language') || 'zh-CN';

// 切换语言函数
function switchLanguage(lang) {
    currentLanguage = lang;
    localStorage.setItem('language', lang);
    
    // 更新HTML语言属性
    document.documentElement.lang = lang;
    
    // 更新所有带有data-i18n属性的元素
    document.querySelectorAll('[data-i18n]').forEach(element => {
        const key = element.getAttribute('data-i18n');
        if (translations[lang] && translations[lang][key]) {
            element.textContent = translations[lang][key];
        }
    });
    
    // 更新语言切换按钮状态
    document.querySelectorAll('.language-switcher button').forEach(button => {
        if (button.getAttribute('data-lang') === lang) {
            button.classList.add('active');
        } else {
            button.classList.remove('active');
        }
    });
    
    // 更新页面标题和描述
    updateMetaTags(lang);
}

// 更新页面元标签
function updateMetaTags(lang) {
    if (lang === 'en') {
        document.title = 'Personal Portfolio';
        document.querySelector('meta[name="description"]').content = 'Personal portfolio website - showcasing my projects, skills and experience';
        document.querySelector('meta[name="keywords"]').content = 'portfolio, personal website, project showcase, skills';
    } else {
        document.title = '个人作品集';
        document.querySelector('meta[name="description"]').content = '个人作品集网站 - 展示我的项目、技能和经验';
        document.querySelector('meta[name="keywords"]').content = '作品集, 个人网站, 项目展示, 技能';
    }
}

// 初始化多语言支持
function initI18n() {
    // 获取现有的语言切换器
    const languageSwitcher = document.querySelector('.language-switcher');
    
    if (languageSwitcher) {
        // 设置初始活动状态
        languageSwitcher.querySelectorAll('button').forEach(button => {
            if (button.getAttribute('data-lang') === currentLanguage) {
                button.classList.add('active');
            } else {
                button.classList.remove('active');
            }
            
            // 添加点击事件
            button.addEventListener('click', () => {
                const lang = button.getAttribute('data-lang');
                switchLanguage(lang);
            });
        });
    }
    
    // 应用当前语言
    switchLanguage(currentLanguage);
}

// 当DOM加载完成后初始化
document.addEventListener('DOMContentLoaded', initI18n);